<?php
if (isset($_SESSION['X_USER'])){
$objCustomerForm = new BM_PHPForm();
$objCustomerForm->UseLocator();
$objCustomerForm->SetIndent("          ");

if (isset($_REQUEST['msg']) && $_REQUEST['msg'] == "add"){
	$objCustomerForm->SetInfor("submit" , "Congratulations! You Are Registered Now.");
}
$entUser = $_SESSION['X_USER'];
$entUser = UserController::find($entUser->username);
$entCustomer = CustomerController::find($entUser->username);

if ($objCustomerForm->IsPost()){
	$objCustomerForm->ChEmptiness("name", "Name is empty.");
	$objCustomerForm->ChWithRegex("name", "Name Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-\s])*$/");	
	$objCustomerForm->ChWithRegex("address", "Address have Invalid chars like '.","/^([^'])*$/");
	$objCustomerForm->ChEmptiness("email", "Email is empty.");
	$objCustomerForm->ChWithRegex("email", "Email is Invalid.", "/^[a-z0-9_\\.\\-]+@+[a-z0-9_\\.\\-]+(\\.[a-z]{2,4})$/");
	$objCustomerForm->ChWithRegex("phone"	, "Phone have ['] Please Remove ' of your text.","/^([^'])*$/");
	$objCustomerForm->ChWithRegex("cell"	, "Cell have ['] Please Remove ' of your text.","/^([^'])*$/");
//	$objCustomerForm->ChWithRegex("birthday", "Birthday have ['] Please Remove ' of your text.","/^([^'])*$/");
//	$objCustomerForm->ChEmptiness("type", "Please Select Category .");
	
	if ($objCustomerForm->IsValid()){
		$entUser = new User($entUser->username,"",$objCustomerForm->arrPostData['name']);
		$entCustomer = new Customer($entUser->username,$objCustomerForm->arrPostData['address'],$objCustomerForm->arrPostData['email'],$objCustomerForm->arrPostData['phone'],$objCustomerForm->arrPostData['cell'],null,null);
		if(UserController::update($entUser) && CustomerController::update($entCustomer)){
			$objCustomerForm->SetInfor("submit","Your Information Updated Successfully !");
		}else{
			$objCustomerForm->SetInfor("submit","Nothing Updated, Error in your input data!");
		}
	}	
}

print $objCustomerForm->ErrMsgs();
print $objCustomerForm->InfMsgs();

print $objCustomerForm->Open("customerform","Update Profile");
print $objCustomerForm->HiddenField("action" ,"$action");
print $objCustomerForm->TextBox("username" ,"Username :",$entUser->username,array('readonly'=>'readonly'));
print $objCustomerForm->TextBox("name" ,"Name&Family :",$entUser->name);
print "		<div style=\"padding-left: 40px;padding-bottom: 10px;\">Password :  <a href=\"../$www/index_customer.php?action=chpass\">Change Password</a></div>\r\n";
print $objCustomerForm->TextBox("address" 	,"Address :",$entCustomer->address);
print $objCustomerForm->TextBox("email" 	,"E-mail :",$entCustomer->email);
print $objCustomerForm->TextBox("phone" 	,"Phone No :",$entCustomer->phone);
print $objCustomerForm->TextBox("cell" 		,"Mobile No :",$entCustomer->cell);
//print $objCustomerForm->TextBox("birthday" 	,"Date Of Birth :",$entCustomer->birthday);
//print $objCustomerForm->Select ("type" 		,"Category :",$arrCtype,array(),$entCustomer->type);

print $objCustomerForm->SubmitButton("submit", "Update", array(), "submit");
print $objCustomerForm->Close();
}else{
print "<h1 style=\"color:red\">Access Denied.<h1>";	
}
?>